package com.shenzhen.horselive.base;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.appcompat.widget.Toolbar;

import com.gyf.immersionbar.ImmersionBar;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.common.utils.Utils;
import com.shenzhen.horselive.presenter.BasePresenter;
import com.shenzhen.horselive.widget.ToolBarHelper;


/**
 * Created by lu on 2016/6/14.
 */
public abstract class AppBarActivity<T extends BasePresenter> extends BaseActivity<T> {
    private ToolBarHelper toolBarHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        toolBarHelper = new ToolBarHelper(this, layoutResID);
        super.setContentView(toolBarHelper.getContentView());
        /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolBarHelper.getToolBar());
//        toolBarHelper.setTitle(getTitle());
        toolBarHelper.setNavigationIcon(R.drawable.ic_back);
        initToolBarEvent();
    }

    @Override
    public void setContentView(View view) {
        toolBarHelper = new ToolBarHelper(this, view);
        super.setContentView(toolBarHelper.getContentView());
        /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolBarHelper.getToolBar());
//        toolBarHelper.setTitle(getTitle());
        toolBarHelper.setNavigationIcon(R.drawable.ic_back);
        initToolBarEvent();
    }

    @Override
    protected void setTranslucentStatus() {
        ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.colorPrimary).init();
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        if (toolBarHelper != null) {
            toolBarHelper.setTitle(title);
        }
    }

    @Override
    public void setTitle(int titleId) {
        super.setTitle(titleId);
        if (toolBarHelper != null) {
            toolBarHelper.setTitle(titleId);
        }
    }


    private void initToolBarEvent() {
        toolBarHelper.setNavigationOnClickListener(onNavigationClickListener);
        toolBarHelper.setNavigationTextClickListener(onNavigationClickListener);
        toolBarHelper.setOnMenuTextClickListener(onMenuClickListener);
    }

    protected void hideNavigationIcon() {
        toolBarHelper.hideNavigationIcon();
    }

    public void hideToolbar() {
        getSupportActionBar().hide();
        toolBarHelper.hideToolBar();
    }

    protected void setMenuIconVisible(boolean visible) {
        toolBarHelper.setMenuIconVisible(visible);
        supportInvalidateOptionsMenu();
    }

    protected void setMenuText(int textResId) {
        toolBarHelper.setMenuTextVisibility(View.VISIBLE);
        toolBarHelper.setMenuText(textResId);
    }

    protected void setMenuText(String title) {
        toolBarHelper.setMenuTextVisibility(View.VISIBLE);
        toolBarHelper.setMenuText(title);
    }

    protected void setMenuTextVisibility(int visibility) {
        toolBarHelper.setMenuTextVisibility(visibility);
    }


    protected void setMenuIcon(int drawableResId) {
        toolBarHelper.setMenuIcon(drawableResId);
    }

    protected void setNavigationIcon(int imgId) {
        toolBarHelper.setNavigationTextVisibility(View.GONE);
        toolBarHelper.setNavigationIcon(imgId);
    }

    public void setNavigationText(String text) {
        toolBarHelper.setNavigationTextVisibility(View.VISIBLE);
        toolBarHelper.setNavigationText(text);
        toolBarHelper.setNavigationIcon(null);
    }


    public View.OnClickListener onNavigationClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onNavigationViewClick();
        }
    };

    protected void onNavigationViewClick() {
        back();
    }

    public View.OnClickListener onMenuClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onActionMenuViewClick();
        }
    };

    protected void back() {
        try {
            onBackPressed();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        if (toolBarHelper.hasMenuIcon()) {
            menu.add(Menu.NONE, R.id.menu, 0, null)
                    .setEnabled(true)
                    .setIcon(toolBarHelper.getMenuIcon())
                    .setActionView(null)
//                    .setShowAsActionFlags()
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        }
        Utils.ignoreMenuLongClick(this, R.id.menu);
        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (toolBarHelper.hasMenuIcon()) {
            menu.findItem(R.id.menu).setVisible(toolBarHelper.isMenuIconVisible());

        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (toolBarHelper.hasMenuIcon()) {
            if (item.getItemId() == R.id.menu) {
                onActionMenuViewClick();
            }
        }
        return super.onOptionsItemSelected(item);
    }

    protected void onActionMenuViewClick() {

    }

    public Toolbar getToolbar() {
        return toolBarHelper.getToolBar();
    }

    public void setStyleDark() {
        ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.white).init();
        toolBarHelper.setStyleWhite();
    }

    public void setStyleRed() {
        ImmersionBar.with(this).statusBarDarkFont(false).statusBarColor(R.color.red_common).init();
        toolBarHelper.setStyleRed();
    }


    public void setStyleRedWithOutBack() {
        ImmersionBar.with(this).statusBarDarkFont(false).statusBarColor(R.color.red_common).init();
        toolBarHelper.setStyleRedWithoutBack();
    }


}
